/*
 *   This file is part of verbum.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 	 Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */

fileListGrants = "assign";

var fileStatusCoordDs = new Ext.data.JsonStore({
    url: getMainUrl(),
    method: 'POST',
    baseParams: {action: 'system_list', op: "assign_coord"},
    autoLoad: false,
    root: 'results',
    id:'id',
    fields:[
     	{name: 'id' },
        {name: 'text', mapping: 'id', convert: translate }
    ]
});

fileStatusCoordDs.load();

function validateAssignation(value1, value2) {
	var r = false;
    if ((value1=="" && value2=="") || (value1==undefined && value2==undefined)) {
		Ext.Msg.message(_('Either translator or reviewer are required.'));
	} else {
		if (value1==value2 && (value1!="none" && value2!="none")) {
			Ext.Msg.message(_('You should not set the same user as translator and reviewer.'));
		} else {
			r = true;
		}
	}
	return r;
}

Ext.extend(Verbum.list.AssignWindow, Ext.util.Observable, {

    show : function(){
    	var scope = this;
    	var desktop = scope.desktop;
	    var formwin='filelistAssign_'+scope.filePid;
		var fileListEditWin = desktop.getWindow(formwin);
				
		if (!fileListEditWin) {
			var UserListDS = new Ext.data.JsonStore({
			    id: 'UserListDS',
				url: getMainUrl(),
				method: 'POST',
			    baseParams: {action: 'system_list', op: 'user'},
			    root: 'results',
			    autoLoad: false,
			    fields:[
					{name: 'userid'},
					{name: 'value'},
			    ]
			});
			UserListDS.load();
					
		    var fileListForm = new Ext.form.FormPanel({
		        url: scope.url,
		        labelWidth: 100,
		        waitMsgTarget: true,
		        frame: true,
		        items: [{
		        	    xtype : "fieldset",
						autoHeight : true,
						items : [
							{
								xtype: 'combo',
								fieldLabel: _('Translator'),
								hiddenName: 'userid1',
								name: 'user1',
								emptyText: _('Select an user...'),
								editable: false,
								forceSelection: true,
								triggerAction: 'all',
							   	store: UserListDS,
							   	valueField: 'value',
							   	mode: 'local',
							   	displayField: 'userid',
							   	width: 170
							},{
								xtype: 'combo',
								fieldLabel: _('Reviewer'),
								hiddenName: 'userid2',
								name: 'user2',
								emptyText: _('Select an user...'),
								editable: false,
								forceSelection: true,
								triggerAction: 'all',
							   	store: UserListDS,
							   	valueField: 'value',
							   	mode: 'local',
							   	displayField: 'userid',
							   	width: 170
							},{
								xtype: 'textarea',
								fieldLabel: _('Comments'),
								name: 'comment',
								width: 170,
								maxLength: 255,
								maxLengthText: _('The maximum length for this field is {0}')
							},{
								xtype: 'combo',
								fieldLabel: _('Force status'),
								hiddenName: 'statusid',
								emptyText: _('(optional)'),
								editable: false,
								allowBlank: true,
								forceSelection: true,
								triggerAction: 'all',
							   	store: fileStatusCoordDs,
							   	valueField: 'id',
							   	displayField: 'text',
							   	width: 170,
							   	mode: 'local'								
							}
							/*
							,{
								xtype: 'checkbox',
								name: 'initstatus',
								hideLabel: true,
								ctCls: 'nooverflow',
								boxLabel: String.format(_('Set initial status to {0} (only applicable if status is {1}) -not required-'),translate(FILE_STATUS_CHKPND),translate(FILE_STATUS_TODO)),
							}*/							
							]
		        }], 
				tbar: [{
					text: _('Save'),
					tooltip: _('Save'),
					iconCls: 'save',
					handler: function() {
							var selections = scope.fileList.selModel.getSelections();
						  	var encoded_array = "[";
						  	for(i = 0; i< scope.fileList.selModel.getCount(); i++){
						  		var ss = "{\"id\":\"" +selections[i].get('id') + "\",\"hash\":\"" + selections[i].get('chstamp') + "\"}";
								if (i==0) {
									encoded_array = encoded_array + ss;
								} else {
									encoded_array = encoded_array + "," + ss;
								}
						  	}
						  	encoded_array = encoded_array + "]";
	
		                	fileListForm.initFields();
		                	
		                	var value1 = fileListForm.getForm().findField('userid1').value;
		                	var value2 = fileListForm.getForm().findField('userid2').value;
							var value3 = fileListForm.getForm().findField('statusid').value;

	                   		if ((value1==undefined && value2==undefined && value3!=undefined)
	                   				|| validateAssignation(value1,value2)) {
	                   			
	                    		fileListForm.getForm().submit({ 
		                        	method:'POST', 
		                        	waitTitle: _('Saving...'), 
		                        	waitMsg: _('Saving...'),
									params: {ids: encoded_array, pid: scope.filePid, op: 'assign'},
			                        success:function(form, action){
			                        	obj = Ext.util.JSON.decode(action.response.responseText);
			                        	if (obj.ret!=undefined) scope.fireEvent("refresh",obj.ret.results);
			                        	else scope.fireEvent("refresh",[]);
			                        	fileListEditWin.close();
			                        },
			                        failure:function(form, action){ 
		                                obj = Ext.util.JSON.decode(action.response.responseText); 
		                                Ext.Msg.error(obj.error);
			                        	if (obj.ret!=undefined) scope.fireEvent("refresh",obj.ret.results);
			                        	else scope.fireEvent("refresh",[]);
		                                fileListEditWin.close(); 
			                        }                   	
			                    });
		                    }
						}
					}]		        	
			});        	
			
			
		    fileListEditWin = desktop.createWindow({
	            id: formwin,
	            layout:'fit',
	            title:_('Assign Files'),
	            width:350,
	            height:270,
	            resizable: false,
	            maximizable: false,
	            items: fileListForm
		    });
		    /*
		    fileListEditWin = new Ext.Window({
	            id: formwin,
	            layout:'fit',
	            title:_('Assign Files'),
	            width:350,
	            height:200,
	            resizable: false,
	            maximizable: false,
	            items: fileListForm
		    });
		    	*/
			fileListEditorWindows[0] = fileListEditWin;
 
		}
		fileListEditWin.show();        
    }
});